#pragma once
class Space
{
public:
	Space();
	~Space();
	static double** CreateDouble(int n1, int n2);
	
	static void DeleteDouble(double **p, int n1);

	template<class T>
	static T** Create(int n1,int n2);
	template<class T>
	static void Delete(T** p, int n1);
	template<class T>
	static void Set(T** p, int n1, int n2, T t);
	template<class T>
	static void Set(T**p1, int n1, int n2, T** p2);
	template<class T>
	static T* Catch(T* p1, int n1, T* p2, int n2, int& n);
	template<class T>
	static T*** Create(int n1, int n2, int n3);
	template<class T>
	static void Delete(T*** p, int n1, int n2);
	template<class T>
	static void Set(T*** p, int n1, int n2, int n3, T t);
};

template<class T>
inline T ** Space::Create(int n1, int n2)
{
	T **p = new T*[n1];
	for (int i = 0; i < n1; i++)
	{
		p[i] = new T[n2];
	}
	return p;
}

template<class T>
inline void Space::Delete(T ** p, int n1)
{
	for (int i = 0; i < n1; i++)
	{
		delete[] p[i];
	}
	delete[]p;
}

template<class T>
inline void Space::Set(T ** p, int n1, int n2, T t)
{
	for (int i = 0; i < n1; i++)
	{
		for (int j = 0; j < n2; j++)
		{
			p[i][j] = t;
		}
	}
}

template<class T>
inline void Space::Set(T ** p1, int n1, int n2, T ** p2)
{
	for (int i = 0; i < n1; i++)
	{
		for (int j = 0; j < n2; j++)
		{
			p1[i][j] = p2[i][j];
		}
	}
}

template<class T>
inline T * Space::Catch(T * p1, int n1, T * p2, int n2, int& n)
{
	n = n1 + n2;
	T* p = new T[n];
	int i;
	for (i = 0; i < n1; i++)
	{
		p[i] = p1[i];
	}
	for (int j = 0; j < n2; j++)
	{
		p[i + j] = p2[j];
	}
	delete[]p1;
	delete[]p2;
	return p;
}

template<class T>
inline T *** Space::Create(int n1, int n2, int n3)
{
	T ***p = new T**[n1];
	for (int i = 0; i < n1; i++)
	{
		p[i] = new T*[n2];
		for (int j = 0; j < n2; j++)
		{
			p[i][j] = new T[n3];
		}
	}
	return p;
}

template<class T>
inline void Space::Delete(T *** p, int n1, int n2)
{
	for (int i = 0; i < n1; i++)
	{
		for (int j = 0; j < n2; j++)
		{
			delete[] p[i][j];
		}
		delete[] p[i];
	}
	delete[]p;
}

template<class T>
inline void Space::Set(T *** p, int n1, int n2, int n3, T t)
{
	for (int i = 0; i < n1; i++)
	{
		for (int j = 0; j < n2; j++)
		{
			for (int k = 0; k < n3; k++)
			{
				p[i][j][k] = t;
			}
		}
	}
}
